﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 陈明
{
    class DBHelper
    {
        private string connstr = "server =.;database =ad;uid=sa;pwd=123456";
        private static string sql = "select * from Userinfo";

        public static void UserinfoReader()
        {
            string connectionString = "server =.;database =ad;uid=sa;pwd=123456";

            SqlConnection sqlConnection = new SqlConnection(connectionString);

            sqlConnection.Open();

            var sqlCommandString = "select *from UserName";
            sqlCommand sqlCommand = new sqlCommand(sqlCommandString, sqlConnection);

            var sdr = sqlCommand.ExecuteReader();

            Console.WriteLine("{0}\t{1}\t{2}", "名字", "年龄", "性别");

            while (sdr.Read())
            {
                Console.WriteLine("{0}\t{1}\t{2}", sdr["Name"], sdr["Age"], sdr["Sex"]);
            }

            sqlConnection.Close();

        }

        public static void UserinfoAdapter()
        {
            DataTable dataTable = new DataTable();
            //获得连接
            SqlConnection conn = new SqlConnection(connstr);

            conn.Open();
            //适配器对象
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            sda.Fill(dataTable);
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t", dataTable.Rows[i]["id"], dataTable.Rows[i]["sex"], dataTable.Rows[i]["age"]);
            }
        }
    }
}
